var dbConfig = require('../util/dbconfig')

// ---------------------------------增
insertArticle = (req, res) => {
    var { title, type_id, original, content } = req.body
    var date = new Date();
    var nowTime = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
    var sql = "insert into article(id, title, type_id, original, content,create_time) value(?,?,?,?,?,?)";
    var sqlArr = [Math.random().toString(36).substr(-10), title, type_id, original, content, nowTime];
    var callBack = (err, data) => {
        if (err) {
            res.send({
                code: 400,
                msg: '添加失败',
            })
            console.log(err)
        } else {
            res.send({
                code: 200,
                msg: '添加成功',
            })
        }
    }
    dbConfig.sqlConnect(sql, sqlArr, callBack);
}
// ---------------------------------删
deleteArticle = (req, res) => {
    let { id } = req.query;
    let sql = 'delete from article where id = ?'
    let sqlArr = [id]
    var callBack = (err, data) => {
        if (err) {
            res.send({
                code: 400,
                msg: '删除失败！！',
                'list': data
            })
        } else {
            res.send({
                code: 200,
                msg: '删除成功！！',
            })
        }
    }
    dbConfig.sqlConnect(sql, sqlArr, callBack);
}
// ---------------------------------改
let getArticleSign = async (req, res) => {
    let { id, sign } = req.query;
    // console.log(req)
    let sql = 'UPDATE article SET sign = ? WHERE id = ?';
    let sqlArr = [sign, id];
    let result = await dbConfig.SySqlConnect(sql, sqlArr)
    console.log(sqlArr)
    if (result.affectedRows === 1) {
        res.send({
            'code': 200,
            msg: '查看次数+1'
        })
    } else {
        res.send({
            'code': 400,
            msg: '+1失败'
        })
    }
}
// ---------------------------------查
// 获取所有文章内容
getArticle = (req, res) => {
    var sql = "select * from article order by create_time desc";
    var sqlArr = [];
    var callBack = (err, data) => {
        if (err) {
            res.send({
                code: 400,
                msg: '获取出错！',
                'list': data
            })
        } else {
            res.send({
                code: 200,
                msg: 'success',
                'list': data
            })
        }
    }
    dbConfig.sqlConnect(sql, sqlArr, callBack);
},
    // 根据ID查询数据
    getIdArticle = (req, res) => {
        var { id } = req.query;
        var sql = "select * from article where id=?";
        var sqlArr = [id];
        var callBack = (err, data) => {
            if (err) {
                res.send({
                    code: 400,
                    msg: '获取出错！',
                    'list': data
                })
            } else {
                res.send({
                    code: 200,
                    msg: 'success',
                    'list': data
                })
            }
        }
        dbConfig.sqlConnect(sql, sqlArr, callBack);
    },
    // 根据文章类型获取数据
    getTypeArticle = (req, res) => {
        var { type_id } = req.query;
        var sql = `select * from article where type_id=? order by create_time desc `;
        var sqlArr = [type_id];
        var callBack = (err, data) => {
            if (err) {
                res.send({
                    code: 400,
                    msg: '获取出错！',
                    'list': data
                })
            } else {
                res.send({
                    code: 200,
                    msg: 'success',
                    'list': data
                })
            }
        }
        dbConfig.sqlConnect(sql, sqlArr, callBack);
    }

module.exports = {
    getArticle,
    getIdArticle,
    getTypeArticle,
    insertArticle,
    deleteArticle,
    getArticleSign
} 